package com.aaa.ms.service.impl;

import com.aaa.common.bo.Order;
import com.aaa.ms.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * fileName:OrderServiceImpl
 * description:
 * author: Mr.Ge
 * createTime:2020/11/19 11:36
 * version:1.0.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;
    @Override
    @HystrixCommand(fallbackMethod = "fallBackQueryAll")
    public List<Order> queryAll() {
        return restTemplate.getForObject(
                "http://orderService/order/queryAll",List.class);
    }

    /**
     * 备份方法远程服务一旦当即，该方法起效，要求返回值和参数都要原方法
     * @return
     */
    private List<Order> fallBackQueryAll(){
    List<Order> orders = new ArrayList<>();
    Order order = new Order();
    order.setId(44444);
    order.setMemberUsername("远程服务器宕机，下课！");
    orders.add(order);
    return orders;
}
    @Override
    public Order queryById(Integer id) {
        return restTemplate.getForObject(
                "http://orderService/order/selectOne?id="+id,Order.class,String.class);
    }
}
